﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace AutoTLBB.Utilites {
	public class XmlSerializeHelper {
		public static void SerializeUserConfig(CharacterModel model) {
			XmlSerializer serializer = new XmlSerializer(typeof(CharacterModel));
			if(!Directory.Exists(Application.StartupPath + @"\Configs")) {
				Directory.CreateDirectory(Application.StartupPath + @"\Configs");
			}
			string fileName = Application.StartupPath + string.Format("{0}{1}.xml", @"\Configs\", model.ID);

			using(TextWriter writer = new StreamWriter(fileName)) {
				serializer.Serialize(writer, model);
				writer.Flush();
			}
		}

		public static CharacterModel DeSerializeUserConfig(string modelId) {
			XmlSerializer serializer = new XmlSerializer(typeof(CharacterModel));
			string fileName = Application.StartupPath + string.Format("{0}{1}.xml", @"\Configs\", modelId);
			if(File.Exists(fileName)) {
				try {
					using(FileStream writer = new FileStream(fileName, FileMode.Open)) {
						return (CharacterModel) serializer.Deserialize(writer);
					}
				} catch(Exception e) {
					MessageBox.Show("Có lỗi xẩy ra khi load config file cho nhân vật này!");
				}
			}
			return null;
		}
	}
}
